package androcr.client.mobile.services.cameraviewer;

import java.io.IOException;

import androcr.client.mobile.core.Controller;
import androcr.client.mobile.display.cameraviewer.ICameraActivity;
import androcr.client.mobile.display.cameraviewer.Preview;
import androcr.client.mobile.exceptions.MyJSONException;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.util.Log;
import android.view.View;

public class CameraPresenter implements ICameraPresenter {
	private static final String TAG = "CameraPresenter";
	
	private ICameraActivity cameraActivity;
	
	private Preview preview;
	
	private ShutterCallback shutterCallback;
	private PictureCallback rawCallback;
	private PictureCallback jpegCallback;
	private AutoFocusCallback autoFocusCallback;
	
	private AlertDialog alertDialog;
	
	
	public CameraPresenter(ICameraActivity cameraActivity) {
		this.cameraActivity = cameraActivity;
		this.alertDialog = new AlertDialog.Builder((Context) cameraActivity).create();
		preview = new Preview((Context) cameraActivity);
		
		shutterCallback = new ShutterCallback() {
			public void onShutter() {
				Log.d(TAG, "onShutter called");
			}
		};

		//Handles data for raw picture
		rawCallback = new PictureCallback() {
			public void onPictureTaken(byte[] data, Camera camera) {
				Log.d(TAG, "onPictureTaken called (raw)");
			}
		};

		//Handles data for jpeg picture
		jpegCallback = new PictureCallback() {
			public void onPictureTaken(byte[] data, Camera camera) {
				Log.d(TAG, "onPictureTaken called (jpeg)");
				
				processPicture(data);
			}
		};
		
		autoFocusCallback = new AutoFocusCallback(){
			  @Override
			  public void onAutoFocus(boolean arg0, Camera arg1) {
				  Log.d(TAG, "onAutoFocus called " + arg0);
			  }};
	}


	@Override
	public View getPreview() {
		return preview;
	}


	@Override
	public void processPicture(byte[] data) {
		Controller.getInstance().startPictureProcessor((Context) cameraActivity, data);
	}
	
	@Override
	public void autoFocusAndTakePicture() {
		preview.getCamera().autoFocus(new Camera.AutoFocusCallback() {
			  public void onAutoFocus(boolean success, Camera camera) {
				  camera.takePicture(shutterCallback, null, jpegCallback);
			  }
			});  
	}


	@Override
	public void releaseCamera() {
		if (preview.getCamera() != null){
			preview.getCamera().release();
		}
	}


	@Override
	public void resumeCamera() throws MyJSONException {
		try {
			Log.i(TAG, "preview " + preview);
			Log.i(TAG, "camera " + preview.getCamera());
			preview.getCamera().reconnect();
		} catch (IOException e) {
			throw new MyJSONException(MyJSONException.CAMERA_ERROR);
		}
	}


	@Override
	public void showAlert(String message) {

		alertDialog.setTitle("Alert");
		alertDialog.setMessage(message);
		// alertDialog.setIcon(R.drawable.icon);
		alertDialog.setButton("OK",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						alertDialog.dismiss();
					}
				});
		alertDialog.show();
	}

}
